package org.benjamin.lending.trusteeship.sumapay;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.entity.BorrowInfo;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.TrusteeshipRequest;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>普通项目放款</p>
 * @author 黄文杰
 */
@Component("sumapayLoanOperation")
public class LoanOperation extends SumapayAbstractOperation {

    private final Logger logger = LogManager.getLogger();
    @Override
    protected void processRequest(Map<String, Object> requestMap, MemberInfo memberInfo, BorrowInfo borrowInfo, Map<String, Object> parameterMap) {
        BigDecimal amount = new BigDecimal(parameterMap.get("amount").toString());
        requestMap.put("projectCode",borrowInfo.getNo());
        requestMap.put("sum",amount);
        requestMap.put("payType",2);
        List<Subledger> subledgerList = new ArrayList<Subledger>();

        try {
            requestMap.put(this.SUBLEDGER_LIST,this.objectMapper.writeValueAsString(subledgerList));
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Override
    protected String signatureRequest(Map<String, Object> requestMap) {
        String[] keys = ArrayUtils.addAll(this.basicRequest,"projectCode","sum","payType",this.SUBLEDGER_LIST,
                "noticeUrl","mainAccountType","mainAccountCode");
        return null;
    }

    @Override
    protected void afterRequest(Map<String, Object> parameterMap, MemberInfo memberInfo, BorrowInfo borrowInfo, TrusteeshipRequest trusteeshipRequest) {
        this.abutmentUrl = sumaplayConfig.getServiceName()+"main/TransactionForFT_loan";
    }

    @Override
    protected String[] signatureResponse(Map<String, Object> responseMap) {
        String[] keys = {"requestId","projectCode","result"};
        return this.buildSignature(responseMap,keys);
    }
}
